package com.ht.baselibs.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ht.baselibs.R;
import com.ht.baselibs.bean.SelectItemBean;
import com.ht.baselibs.ui.adapter.SelectItemAdapter;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangtao on 16/9/18.
 */
public class SelectListActivity extends BaseActivity {
    public static final String CENTER_TITLE = "center_title";
    public static final String DATA_LIST = "data_list";
    public static final String DEFAULT_INDEX = "default_index";
    public static final String IS_MUTILTY_SELECT = "is_mutilty_select";

    private String mCenterTitle;
    private boolean isMutitySelect;
    private ArrayList<String> mdataList;
    private List<Integer> mDefaultIndexList;
    private ListView mListView;
    private ArrayList<String> mData;
    private ArrayList<SelectItemBean> mDataList;
    private SelectItemAdapter mAdapter;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mCenterTitle = callIntent.getStringExtra(CENTER_TITLE);
        isMutitySelect = callIntent.getBooleanExtra(IS_MUTILTY_SELECT, false);

        mdataList = callIntent.getStringArrayListExtra(DATA_LIST);

        mDefaultIndexList = new ArrayList<>();
        if (isMutitySelect){
            String defaultIndexStr = callIntent.getStringExtra(DEFAULT_INDEX);
            if (defaultIndexStr != null){
                String[] defaultIndexs = defaultIndexStr.split(",");
                int length = defaultIndexs.length;
                for (int i = 0; i < length; i++) {
                    mDefaultIndexList.add(Integer.parseInt(defaultIndexs[i]));
                }
            }
        } else {
            int defaultIndex = callIntent.getIntExtra(DEFAULT_INDEX, -1);
            if (defaultIndex != -1){
                mDefaultIndexList.add(defaultIndex);
            }
        }

        mData = callIntent.getStringArrayListExtra(DATA_LIST);
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText(mCenterTitle);
        if (isMutitySelect){
            mTitleBarView.addActions(new TitleBarView.Action("确定") {
                @Override
                public void performAction(View view) {

                    StringBuilder sb = new StringBuilder();

                    int size = mdataList.size();
                    for (int i = 0; i < size; i++) {
                        SelectItemBean selectItemBean = mDataList.get(i);
                        sb.append(selectItemBean.isSelect?i+",":"");
                    }

                    sb.deleteCharAt(sb.length()-1);

                    Intent intent = new Intent();
                    intent.putExtra("data", sb.toString());
                    setResult(RESULT_OK, intent);
                    ActivityUtils.finishActivity(SelectListActivity.this);
                }
            });
        }
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_select_list;
    }

    @Override
    protected void initChildView() {
        mListView = (ListView) findViewById(R.id.lv_activity_select_list);
    }

    @Override
    protected void initChildData() {
        mDataList = new ArrayList<>();
        int size = mData.size();
        for (int i = 0; i < size; i++) {
            String title = mData.get(i);
            if (mDefaultIndexList.contains(i)){
                mDataList.add(new SelectItemBean(title, true));
            }else{
                mDataList.add(new SelectItemBean(title));
            }
        }

        mAdapter = new SelectItemAdapter(this, mDataList);
        mListView.setAdapter(mAdapter);
    }

    @Override
    protected void setChildViewListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!isMutitySelect) {
                    Intent intent = new Intent();
                    intent.putExtra("data", position);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    SelectItemBean selectItemBean = mDataList.get(position);
                    selectItemBean.isSelect = !selectItemBean.isSelect;
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}
